from fastapi import APIRouter

from mysql.connection import get_connection
from routers.namedEntity.namedEntityModels import (NamedEntityCreateModel, NamedEntityDeleteModel,
                                                   NamedEntityUpdatenameModel, NamedEntityUpdatedescripModel,
                                                   NamedEntityUpdatetypeModel)

router = APIRouter()

@router.post("/create_entity")
async def __create_entity(namedEntity_create_model: NamedEntityCreateModel):
    if namedEntity_create_model.name == None or namedEntity_create_model.description == None or namedEntity_create_model.type == None:
        return {
            "code": 1,
            "detail": "命名实体名称、描述、类型信息不完整"
        }

    connect = get_connection()    #和数据库建立一个传输数据的连接通道
    with connect.cursor() as cursor:
        sql = "INSERT INTO named_entity(name, description, type) VALUES(%s, %s, %s))"
        try:
            cursor.execute(sql, (namedEntity_create_model.name, namedEntity_create_model.description, namedEntity_create_model.type))
            return {
                "code": 0,
                "data": "created id is xxx"
            }
        except:
            return {
                "code": 2,
                "detail": "新建命名实体失败，请重试"
            }

@router.post("/delete_entity")
async def __delete_entity(namedEntity_delete_model: NamedEntityDeleteModel):#前端询问是否删除？
    if namedEntity_delete_model.id == None:
        return {
            "code": 1,
            "detail": "请输入id"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE id=%s AND dirty=0"###判断命名实体是否存在
        cursor.execute(sql, namedEntity_delete_model.id.__str__())
        result = cursor.fetchall()    #获取查询的结果集
        if len(result) == 0:
            return {
                "code": 2,
                "detail": "实体不存在"
            }

        sql = "UPDATE named_entity SET dirty=1 WHERE id=%s"
        try:
            cursor.execute(sql, namedEntity_delete_model.id.__str__())
            return {
                "code": 0,
                "data": "该命名实体xxx删除成功"
            }
        except:
            return {
                "code": 3,
                "detail": "删除失败，请重试。。。"
            }

@router.post("/update_entityname")######################################################判断id是否存在再进行修改
async def __update_entity_name(namedEntity_update_name: NamedEntityUpdatenameModel):#前端直接传入id，并不需要用户输入。
    if namedEntity_update_name.newname == None:
        return {
            "code": 1,
            "detail": "实体名称不能为空"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE id=%s"
        cursor.execute(sql, namedEntity_update_name.id.__str__())#判断id是否存在
        result = cursor.fetchall()
        if len(result) == 0:
            return {
                "code": 2,
                "detail": "实体不存在。。。"
            }

        sql = "UPDATE named_entity SET name=%s WHERE id=%s"
        try:
            cursor.execute(sql, (namedEntity_update_name.newUsername, namedEntity_update_name.id.__str__()))
            return {
                "code": 0,
                "data": "实体xxx名称修改成功"
            }
        except:
            return {
                "code": 3,
                "detail": "修改失败，请重试"
            }

@router.post("/update_entitydescription")######################################################判断id是否存在再进行修改
async def __update_entity_description(namedEntity_update_description: NamedEntityUpdatedescripModel):#前端直接传入id，并不需要用户输入。
    if namedEntity_update_description.newdescription == None:
        return {
            "code": 1,
            "detail": "实体描述为空！"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE id=%s"
        cursor.execute(sql, namedEntity_update_description.id.__str__())#判断id是否存在
        result = cursor.fetchall()
        if len(result) == 0:
            return {
                "code": 2,
                "detail": "实体不存在。。。"
            }

        sql = "UPDATE named_entity SET description=%s WHERE id=%s"
        try:
            cursor.execute(sql, (namedEntity_update_description.newdescription, namedEntity_update_description.id.__str__()))#更新实体描述
            return {
                "code": 0,
                "data": "实体xxx描述修改成功"
            }
        except:
            return {
                "code": 3,
                "detail": "修改失败，请重试"
            }

@router.post("/update_entitytype")######################################################判断id是否存在再进行修改
async def __update_entity_type(namedEntity_update_type_model: NamedEntityUpdatetypeModel):#前端直接传入id，并不需要用户输入。
    if namedEntity_update_type_model.newtype == None:
        return {
            "code": 1,
            "detail": "实体类型为空！"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE id=%s"
        cursor.execute(sql, namedEntity_update_type_model.id.__str__())#判断id是否存在
        result = cursor.fetchall()
        if len(result) == 0:
            return {
                "code": 2,
                "detail": "实体不存在。。。"
            }

        sql = "UPDATE named_entity SET type=%s WHERE id=%s"
        try:
            cursor.execute(sql, (namedEntity_update_type_model.newtype, namedEntity_update_type_model.id.__str__()))#更新实体描述
            return {
                "code": 0,
                "data": "实体xxx类型修改成功"
            }
        except:
            return {
                "code": 3,
                "detail": "修改失败，请重试"
            }

@router.get("/get_entity_by_name")
async def __get_entities_by_name(name):
    if name == None:
        return {
            "code": 1,
            "detail": "查询实体须输入名称"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE name like %s"
        cursor.execute(sql, name)  # 判断名称是否存在
        results = cursor.fetchall()
        entitiesinfo = []
        for entity in results:
            entitiesinfo.append(entity)
        return {
            "code": 0,
            "data": {
                entitiesinfo
            }
    }

@router.get("/get_entity_by_description")
async def __get_entities_by_name(description):
    if description == None:
        return {
            "code": 1,
            "detail": "查询实体须输入描述"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE description like %s"
        cursor.execute(sql, description)  # 判断名称是否存在
        results = cursor.fetchall()
        entitiesinfo = []
        for entity in results:
            entitiesinfo.append(entity)
        return {
            "code": 0,
            "data": {
                entitiesinfo
            }
    }

@router.get("/get_entity_by_type")
async def __get_entities_by_name(type):
    if type == None:
        return {
            "code": 1,
            "detail": "查询实体须输入类型"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE type like %s"
        cursor.execute(sql, type)  # 判断名称是否存在
        results = cursor.fetchall()
        entitiesinfo = []
        for entity in results:
            entitiesinfo.append(entity)
        return {
            "code": 0,
            "data": {
                entitiesinfo
            }
    }

@router.get("/get_entity_by_dirty")
async def __get_entities_by_name(dirty):
    if dirty == None:
        return {
            "code": 1,
            "detail": "查询实体须输入dirty值"
        }
    connect = get_connection()
    with connect.cursor() as cursor:
        sql = "SELECT * FROM named_entity WHERE dirty like %s"
        cursor.execute(sql, dirty)  # 判断名称是否存在
        results = cursor.fetchall()
        entitiesinfo = []
        for entity in results:
            entitiesinfo.append(entity)
        return {
            "code": 0,
            "data": {
                entitiesinfo
            }
    }